package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import bean.Fund;
import bean.FundPriceHistory;
import bean.Model;
import bean.UserType;
import dao.FundDAO;
import dao.FundPriceHistoryDAO;

/*
 * 
 * Created By Sahil Thakkar for Research Fund  Usecase
 */

public class ResearchFundAction extends Action {
	private List<String> errors = new ArrayList<String>();
	private FundDAO fundDAO;
	private FundPriceHistoryDAO fundPriceHistoryDAO;
	
	public ResearchFundAction(Model model) {
		fundDAO = model.getFundDAO();
		fundPriceHistoryDAO = model.getFundPriceHistoryDAO();
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "researchFund.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		String fundStr = null;
		Fund fund = null;
		ArrayList<Double> fundPrice = new ArrayList<Double>();
		List<Date> fundYear = new ArrayList<Date>();
		
		List<FundPriceHistory> lstFundPriceHistory = null;
		
		try {
			fundStr = request.getParameter("researchFund");
			fundStr = fundStr.trim();
			
			errors.addAll(this.getValidationErrors(fundStr));
			if (errors.size() > 0) {
				request.setAttribute("errors", errors);
				return "openResearchFund.do";
			}
			
			if(fundStr != null)
			{
				fund = fundDAO.researchFund(fundStr);
				if(fund != null)
				{
					lstFundPriceHistory = fundPriceHistoryDAO.getFundPriceHistory(fund);
					
					if(lstFundPriceHistory.size() > 0)
					{
						int i = 0;
						for(FundPriceHistory a : lstFundPriceHistory)
						{
							i++;
							long ls = a.getPrice();
							Double d  = ((double) ls/100.0); 
							
							
							fundPrice.add(d);
							System.out.println("  Price :: "+d);
							fundYear.add(a.getPriceDate());
						}
						request.setAttribute("fund", fund);
						request.setAttribute("fundCount", i);
						request.setAttribute("fundPrice", fundPrice);
						request.setAttribute("fundYear", fundYear);

						return "researchFund.jsp";
					}
					else
					{
						errors.add("No Fund prices available for the Fund.");
					}
				}
				else
				{
					errors.add("Fund not found.");
				}
			}
			
			if (errors.size() > 0) {
				request.setAttribute("errors", errors);
				return "openResearchFund.do";
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error.jsp";
		}
		return "login.jsp";
	}
	public List<String> getValidationErrors(String fundStr) {
		 List<String> errors = new ArrayList<String>();
		if (fundStr.length() == 0)
		{
				errors.add("Please enter something to research");
		}
		if (fundStr.matches(".*[<>\"].*"))
		{
			errors.add("User Name may not contain angle brackets or quotes");
		}
        if (errors.size() > 0) 
        	return errors;
        
        return errors;
    }
	
	 @Override
		public UserType getType() {
	    	return UserType.Customer;
		}
}
